﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DataLayer.Infrastructure;
using ModelLayer;

namespace BusinessLayer
{
    public class ProductManager
    {

        public IEnumerable<Product> GetAllProducts()
        {
            using (var uow = new UnitOfWork())
            {
                return uow.ProductRepo.Get(includeProperties: x => x.Category);
            }
        }

        public IEnumerable<Product> GetByCategory(long categoryId)
        {
            using (var uow = new UnitOfWork())
            {
                return uow.ProductRepo.Get(x => x.CategoryId == categoryId);
            }
        }

        public Product GetById(long productId)
        {
            using (var uow = new UnitOfWork())
            {
                return uow.ProductRepo.Get(x => x.ProductId == productId, null, x => x.Category).FirstOrDefault();
            }
        }

        public bool AddUpdate(Product product)
        {
            using (var uow = new UnitOfWork())
            {
                if (product.ProductId > 0)
                {
                    uow.ProductRepo.Update(product);
                }
                else
                {
                    uow.ProductRepo.Insert(product);
                }

                return uow.SaveChanges() > 0;
            }
        }

        public bool Delete(long productId)
        {
            using (var uow = new UnitOfWork())
            {
                var product = uow.ProductRepo.GetById(productId);
                uow.ProductRepo.Delete(product);

                return uow.SaveChanges() > 0;
            }
        }
    }
}
